package com.hxkcloud.patient.utils;

import android.content.Context;
import android.content.Intent;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.controller.EaseUI;
import com.hyphenate.easeui.model.EaseAtMessageHelper;
import com.hyphenate.easeui.model.EaseNotifier;
import com.hyphenate.easeui.utils.EaseCommonUtils;
import com.hxkcloud.patient.R;
import com.hxkcloud.patient.activitys.MessageMainActivity;

import java.util.List;

/**
 * Created by Administrator on 2017/3/1.
 */

public class EaseUiUtil {
    private EaseUiUtil() {

    }

    private Context appContext;
    private static EaseUiUtil instance = null;
    private EaseUI easeUI;

    public synchronized static EaseUiUtil getInstance() {
        if (instance == null) {
            instance = new EaseUiUtil();
        }
        return instance;
    }

    public void init(Context appContext) {
        this.appContext = appContext;
        EaseUI.getInstance().init(appContext, EMClient.getInstance().getOptions());

        easeUI = EaseUI.getInstance();
        setEaseUIProviders();
    }

    public void sendNewMsg(List<EMMessage> messages) {
        for (EMMessage message : messages) {
            // in background, do not refresh UI, notify it in notification bar
            if (!easeUI.hasForegroundActivies()) {
                easeUI.getNotifier().onNewMsg(message);
            }
        }
    }


    public void setEaseUIProviders() {
        easeUI.getNotifier().init(appContext);
        //set notification options, will use default if you don't set it
        easeUI.getNotifier().setNotificationInfoProvider(new EaseNotifier.EaseNotificationInfoProvider() {

            @Override
            public String getTitle(EMMessage message) {
                //you can update title here
                return "您有一条新消息";
            }

            @Override
            public int getSmallIcon(EMMessage message) {
                //you can update icon here
                return R.mipmap.ic_launcher;
            }

            @Override
            public String getDisplayedText(EMMessage message) {
                // be used on notification bar, different text according the message type.
                String ticker = EaseCommonUtils.getMessageDigest(message, appContext);
                if (message.getType() == EMMessage.Type.TXT) {
                    ticker = ticker.replaceAll("\\[.{2,3}\\]", "[表情]");
                }
              //  EMConversation conversation = EMClient.getInstance().chatManager().getConversation(message.getFrom());
                String nickName = message.getStringAttribute("name", "");
                //EaseUser user = getUserInfo(message.getFrom());
                if (nickName != null) {
                    if (EaseAtMessageHelper.get().isAtMeMsg(message)) {
                        return nickName;
                    }
                    return nickName+":"+ticker;
                } else {
                    if (EaseAtMessageHelper.get().isAtMeMsg(message)) {
                        return message.getFrom();
                    }
                    return ticker;
                }
            }

            @Override
            public String getLatestText(EMMessage message, int fromUsersNum, int messageNum) {
                // here you can customize the text.
                // return fromUsersNum + "contacts send " + messageNum + "messages to you";
                String ticker = EaseCommonUtils.getMessageDigest(message, appContext);
                if (message.getType() == EMMessage.Type.TXT) {
                    ticker = ticker.replaceAll("\\[.{2,3}\\]", "[表情]");
                }
                String nickName = message.getStringAttribute("name", "");
                //EaseUser user = getUserInfo(message.getFrom());
                if (nickName != null) {
                    if (EaseAtMessageHelper.get().isAtMeMsg(message)) {
                        return nickName;
                    }
                    return nickName+":"+ticker;
                } else {
                    if (EaseAtMessageHelper.get().isAtMeMsg(message)) {
                        return message.getFrom();
                    }
                    return ticker;
                }
            }

            @Override
            public Intent getLaunchIntent(EMMessage message) {
                // you can set what activity you want display when user click the notification
                Intent intent = new Intent(appContext, MessageMainActivity.class);
//                intent.putExtra(EaseConstant.EXTRA_USER_ID,message.getUserName());
//                try {
//                    intent.putExtra("name",message.getStringAttribute("name"));
//                } catch (HyphenateException e) {
//                    e.printStackTrace();
//                }

                /*// open calling activity if there is call
                if(isVideoCalling){
                    intent = new Intent(appContext, VideoCallActivity.class);
                }else if(isVoiceCalling){
                    intent = new Intent(appContext, VoiceCallActivity.class);
                }else{
                    EMMessage.ChatType chatType = message.getChatType();
                    if (chatType == EMMessage.ChatType.Chat) { // single chat message
                        intent.putExtra("userId", message.getFrom());
                        intent.putExtra("chatType", Constant.CHATTYPE_SINGLE);
                    } else { // group chat message
                        // message.getTo() is the group id
                        intent.putExtra("userId", message.getTo());
                        if(chatType == EMMessage.ChatType.GroupChat){
                            intent.putExtra("chatType", Constant.CHATTYPE_GROUP);
                        }else{
                            intent.putExtra("chatType", Constant.CHATTYPE_CHATROOM);
                        }

                    }
                }*/
                return intent;
            }
        });

    }

}
